package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.*;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.*;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.po.StatsAgency;
import com.huilian.iotbox.data.po.StatsUser;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author 龙 路 飞
 * @CreateDate: 2021-05-09-19-40
 * @Description:
 */
@RestController
@RequestMapping("/api/home")
public class HomeController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private StatsAgencyService statsAgencyService;
    @Autowired
    private StatsUserService statsUserService;

    @Autowired
    private StatsStoreService statsStoreService;

    /**
     * 统计当天的销售额,利润,交易笔数,销量(游戏币)
     *
     * @param request
     * @return
     */
    @GetMapping("/findCountDay")
    public ServerResponse findCountDay(HttpServletRequest request) {
        OrderDto orderDto = new OrderDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        orderDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(orderService.findCountDay(orderDto));
    }


    /**
     * 统计昨天的销售额,利润,交易笔数,销量(游戏币)
     *
     * @param request
     * @return
     */
    @GetMapping("/findCountYesterday")
    public ServerResponse findCountYesterday(HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //如果是管理员和机构登录就走这里
        if (RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()) || RoleEnum.AGENT.getRoleId().equals(userInfo.getRoleId()) || RoleEnum.FINANCE.getRoleId().equals(userInfo.getRoleId())) {
            StatsAgency statsAgency = new StatsAgency();
            statsAgency.setUserInfo(userInfo);
            statsAgency.setDateStr(sdf.format(calendar.getTime()));
            return ServerResponse.createBySuccess(statsAgencyService.findCountYesterday(statsAgency));
        } else if (RoleEnum.MERCHANT.getRoleId().equals(userInfo.getRoleId()) || RoleEnum.SUB_ACCOUNT.getRoleId().equals(userInfo.getRoleId())) {
            //如果是运营商登录就走这里
            StatsUser statsUser = new StatsUser();
            statsUser.setUserInfo(userInfo);
            statsUser.setDateStr(sdf.format(calendar.getTime()));
            return ServerResponse.createBySuccess(statsUserService.findCountYesterday(statsUser));
        }
        return ServerResponse.createByErrorMessage("请求错误!");
    }

    /**
     * 统计每月的销售额
     *
     * @param request
     * @return
     */
    @GetMapping("/findMonthlyTurnover")
    public ServerResponse findMonthlyTurnover(HttpServletRequest request, Integer type) {
        StatsAgencyDto statsAgencyDto = new StatsAgencyDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsAgencyDto.setUserInfo(userInfo);
        statsAgencyDto.setType(type);
        return ServerResponse.createBySuccess(statsAgencyService.findMonthlyTurnover(statsAgencyDto));
    }

    /**
     * 统计每月的订单数
     *
     * @param request
     * @return
     */
    @GetMapping("/findMonthlyOrderCount")
    public ServerResponse findMonthlyOrderCount(HttpServletRequest request, Integer type) {
        StatsAgencyDto statsAgencyDto = new StatsAgencyDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsAgencyDto.setUserInfo(userInfo);
        statsAgencyDto.setType(type);
        return ServerResponse.createBySuccess(statsAgencyService.findMonthlyOrderCount(statsAgencyDto));
    }

    /**
     * 统计每月的利润
     *
     * @param request
     * @return
     */
    @GetMapping("/findMonthlyTotalCommission")
    public ServerResponse findMonthlyTotalCommission(HttpServletRequest request, Integer type) {
        StatsAgencyDto statsAgencyDto = new StatsAgencyDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsAgencyDto.setUserInfo(userInfo);
        statsAgencyDto.setType(type);
        return ServerResponse.createBySuccess(statsAgencyService.findMonthlyTotalCommission(statsAgencyDto));
    }

    /**
     * 统计运营商销售排行前几
     *
     * @param request
     * @return
     */
    @GetMapping("/findUserTurnoverTop")
    public ServerResponse findUserTurnoverTop(HttpServletRequest request, Integer type) {
        StatsUserDto statsUserDto = new StatsUserDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        statsUserDto.setType(type);
        return ServerResponse.createBySuccess(statsUserService.findUserTurnoverTop(statsUserDto));
    }

    /**
     * 统计运营商订单交易笔数排行前几
     *
     * @param request
     * @return
     */
    @GetMapping("/findUserOrderCountTop")
    public ServerResponse findAgencyOrderCountTop(HttpServletRequest request, Integer type) {
        StatsUserDto statsUserDto = new StatsUserDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsUserDto.setUserInfo(userInfo);
        statsUserDto.setType(type);
        return ServerResponse.createBySuccess(statsUserService.findUserOrderCountTop(statsUserDto));
    }

    /**
     * 统计代理商利润排行前几
     *
     * @param request
     * @return
     */
    @GetMapping("/findAgencyTotalCommissionTop")
    public ServerResponse findAgencyTotalCommissionTop(HttpServletRequest request, Integer type) {
        StatsAgencyDto statsAgency = new StatsAgencyDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsAgency.setUserInfo(userInfo);
        statsAgency.setType(type);
        return ServerResponse.createBySuccess(statsAgencyService.findAgencyTotalCommissionTop(statsAgency));
    }


    /**
     * 统计基本运营情况
     *
     * @param request
     * @param type
     * @return
     */
    @GetMapping("/findStateAgency")
    public ServerResponse findStateAgency(HttpServletRequest request, Integer type) {
        Calendar calendar = null;
        calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        StatsAgencyDto statsAgencyDto = new StatsAgencyDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsAgencyDto.setUserInfo(userInfo);
        statsAgencyDto.setType(type);
        statsAgencyDto.setDateStr(sdf.format(calendar.getTime()));
        return ServerResponse.createBySuccess(statsAgencyService.findStateAgency(statsAgencyDto));
    }

    /**
     * 统计商家销售排行前几
     *
     * @param request
     * @return
     */
    @GetMapping("/findMerchantTurnoverTop")
    public ServerResponse findMerchantTurnoverTop(HttpServletRequest request, Integer type) {
        StatsStoreDto statsStoreDto = new StatsStoreDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsStoreDto.setUserInfo(userInfo);
        statsStoreDto.setType(type);
        return ServerResponse.createBySuccess(statsStoreService.findMerchantTurnoverTop(statsStoreDto));
    }
    /**
     * 统计商家销售排行前几
     *
     * @param request
     * @return
     */
    @GetMapping("/findMerchantOrderCountTop")
    public ServerResponse findMerchantOrderCountTop(HttpServletRequest request, Integer type) {
        StatsStoreDto statsStoreDto = new StatsStoreDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        statsStoreDto.setUserInfo(userInfo);
        statsStoreDto.setType(type);
        return ServerResponse.createBySuccess(statsStoreService.findMerchantOrderCountTop(statsStoreDto));
    }

}
